﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using FgoData.Crawler.AppMedia;
using FgoData.Crawler.FgoWiki;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NPOI.OpenXml4Net.OPC;

namespace FgoData.Launcher
{
    class Program
    {
        static IServiceProvider BuildSvcProvider()
        {
            var svcProvider = new ServiceCollection()
                .AddSingleton<FgowikiCrawler>()
                .AddSingleton<AppMediaCrawler>()
                .AddSingleton<FgoDataHelper>()
                .AddLogging()
                .BuildServiceProvider();
            return svcProvider;
        }
        
        static void Main(string[] args)
        {
            var svcProvider = BuildSvcProvider();

            svcProvider.GetService<ILoggerFactory>()
                .AddConsole(LogLevel.Debug);

            ISet<string> argSet = new HashSet<string>();
            
            for (int index = 0; index < args.Length; index++)
            {
                Console.WriteLine("args[{0}] = {1}", index, args[index]);
                argSet.Add(args[index]);
            }
            
            var fgoDataHelper = svcProvider.GetService<FgoDataHelper>();
            
            if (argSet.Contains("--crawl-fgowiki"))
            {
                fgoDataHelper.LoadDataFromFgowiki();
                fgoDataHelper.ApplyErrata();
            }

            if (argSet.Contains("--crawl-appmedia"))
            {
                fgoDataHelper.LoadLeaderBoardFromAppmedia();
                fgoDataHelper.ExportLeaderBoardAsJson();
            }

            if (argSet.Contains("--export-as-excel"))
            {
                fgoDataHelper.ExportFgoDataAsExcel(@"D:\temp\fgodata-20180521.xlsx");
            }
        }
    }
}